<template>
  <div>
    <!-- 添加或修改公告对话框 -->
    <el-dialog title="保养完成" :visible.sync="$parent.open" width="75%" append-to-body>
      <div class="center_dialog children">
        <el-divider>基本信息</el-divider>
        <el-descriptions direction="vertical" :column="4" border>
          <el-descriptions-item label="任务单编号">{{ form.upkeepTaskNum || '-' }}</el-descriptions-item>
          <el-descriptions-item label="申请单号">{{ form.ukPlanPartInfoVo && form.ukPlanPartInfoVo && form.ukPlanPartInfoVo
              &&
              form.ukPlanPartInfoVo.upkeepPlanNum || '-'
          }}</el-descriptions-item>
          <el-descriptions-item label="标题">{{ form.ukPlanPartInfoVo && form.ukPlanPartInfoVo.title || '-' }}
          </el-descriptions-item>
          <!-- <el-descriptions-item label="设备编号">{{ form.equipmentNum || '-' }}</el-descriptions-item>
          <el-descriptions-item label="设备名称">{{ form.equipmentName || '-' }}</el-descriptions-item>
          <el-descriptions-item label="供应商">{{ form.supplierName || '-' }}</el-descriptions-item> -->
          <el-descriptions-item label="保养人">
            <span v-for="(v, i) in form.upkeepPlanMaintainerList" :key="i">
              {{ v.maintainerName || '-' }}
              <template v-if="i !== form.upkeepPlanMaintainerList.length - 1">
                ,
              </template>
            </span>
            <!-- {{ form.ukPlanPartInfoVo && form.ukPlanPartInfoVo.headName || '-' }} -->
          </el-descriptions-item>
          <el-descriptions-item label="保养部门">{{ form.ukPlanPartInfoVo && form.ukPlanPartInfoVo.deptName || '-' }}
          </el-descriptions-item>
          <el-descriptions-item label="循环类型">{{ ['循环保养', '单次保养'][form.ukPlanPartInfoVo && form.ukPlanPartInfoVo &&
              form.ukPlanPartInfoVo && form.ukPlanPartInfoVo.generateTaskType] || '-'
          }}</el-descriptions-item>
          <el-descriptions-item label="生成任务">{{ ['提前一日', '提前二日', '提前三日',
              '提前四日', '提前五日'][form.ukPlanPartInfoVo && form.ukPlanPartInfoVo.generateTaskType]
              || '-'
          }}
          </el-descriptions-item>
          <el-descriptions-item label="备注">{{ form.ukPlanPartInfoVo && form.ukPlanPartInfoVo.remark || '-' }}
          </el-descriptions-item>
          <el-descriptions-item label="验收人">{{ form.ukPlanPartInfoVo && form.ukPlanPartInfoVo.acceptorName || '-' }}
          </el-descriptions-item>
          <el-descriptions-item label="验收人部门">{{ form.ukPlanPartInfoVo && form.ukPlanPartInfoVo.acceptanceDeptName ||
              '-'
          }}</el-descriptions-item>
          <el-descriptions-item label="验收方式">{{ form.ukPlanPartInfoVo && form.ukPlanPartInfoVo.acceptanceModeName || '-'
          }}</el-descriptions-item>
          <!-- <el-descriptions-item label="计划保养日期">{{ form.planUpkeepTime || '-' }}</el-descriptions-item>
          <el-descriptions-item label="生成日期">{{ form.generateTime || '-' }}</el-descriptions-item> -->
        </el-descriptions>
        <!-- <el-descriptions :column="1" border>
          <el-descriptions-item label-class-name="left_herder" label="保养状态">
            <span v-for="(value, index) in form.upkeepTaskStatuses" :key="index">
              <dict-tag :options="upkeepStatus" :value="value.upkeepStatus" />
              ({{ value.acceptanceTime || '-' }})
              <span :style="{ 'margin': '0 5px' }" v-if="index !== form.upkeepTaskStatuses.length - 1">/</span>
            </span>
          </el-descriptions-item>
          <el-descriptions-item label="验收状态">
            <span v-for="(value, index) in form.upkeepTaskStatuses" :key="index">
              <dict-tag :options="acceptanceStatus" :value="value.acceptanceStatus" />
              ({{ value.acceptanceTime || '-' }})
              <span :style="{ 'margin': '0 5px' }" v-if="index !== form.upkeepTaskStatuses.length - 1">/</span>
            </span>
          </el-descriptions-item>

          <el-descriptions-item label="验收说明">
            <span v-for="(value, index) in form.upkeepTaskStatuses" :key="index">
              {{ value.explanation }} <span :style="{ 'margin': '0 5px' }"
                v-if="index !== form.upkeepTaskStatuses.length - 1">/</span>
            </span>

          </el-descriptions-item>
        </el-descriptions> -->
        <el-divider>保养标准</el-divider>
        <div v-for="(item, index) in form.upkeepTaskStatuses" :key="index">
          <el-table :data="item.upkeepTaskGenerProVos">
            <el-table-column label="序号" align="center" prop="order" width="50">
              <template slot-scope="scope">
                {{ scope.$index + 1 }}
              </template>
            </el-table-column>
            <el-table-column label="编号" align="center" width="160">
              <template slot-scope="scope">
                {{ scope.row.upkeepType && scope.row.upkeepType.typeNum || '-' }}
              </template>
            </el-table-column>
            <el-table-column label="名称" align="center" min-width="120">
              <template slot-scope="scope">
                {{ scope.row.upkeepType.typeName || '-' }}
              </template>
            </el-table-column>
            <!-- <el-table-column label="周期" align="center" min-width="120">
              <template slot-scope="scope">
                {{ ['一日', '一周', '十五日', '一月', '三月', '六月', '十二月', '不定'][scope.row.upkeepType &&
                    scope.row.upkeepType.cycle] ||
                    '-'
                }}
              </template>
            </el-table-column>
            <el-table-column label="排序" align="center" min-width="120">
              <template slot-scope="scope">
                {{ scope.row.upkeepType && scope.row.upkeepType.sort || '-' }}
              </template>
            </el-table-column> -->
            <!-- <el-table-column label="状态" align="center" min-width="120">
                                            <template slot-scope="scope">
                                              <dict-tag :options="statusFlag" :value="scope.row.status" />
                                            </template>
                                          </el-table-column> -->
            <el-table-column label="备注" align="center" min-width="180">
              <template slot-scope="scope">
                {{ scope.row.upkeepType && scope.row.upkeepType.remark || '-' }}
              </template>
            </el-table-column>
            <el-table-column label="保养状态" align="center" width="100">
              <template slot-scope="scope">
                <el-switch v-model="scope.row.status" @change="handleChange(index, scope.$index, scope.row.status)"
                  active-color="#13ce66" :disabled="!(form.upkeepTaskStatuses.length - 1 === index)"
                  inactive-color="#909399" active-value="1" inactive-value="0">
                </el-switch>
              </template>
            </el-table-column>
            <el-table-column label="保养说明" align="center" min-width="380" show-overflow-tooltip>
              <template slot-scope="scope">
                <template v-if="scope.row.status === '1'">
                  <el-input :disabled="!(form.upkeepTaskStatuses.length - 1 === index)" v-model="scope.row.explanation">
                  </el-input>
                </template>
                <template v-else>
                  {{ scope.row.explanation || '-' }}
                </template>
              </template>
            </el-table-column>
          </el-table>
          <el-divider v-if="index !== form.upkeepTaskStatuses.length - 1">保养项目(重新保养)</el-divider>

        </div>
        <el-divider>配件更换</el-divider>
        <el-form ref="formData" :model="formData" inline :rules="rules" label-width="80px">
          <el-form-item label="换下配件" prop="ecrRemoveRecords">
            <el-select v-model="formData.ecrRemoveRecords" placeholder="请选择换下配件" multiple style="width:100%">
              <el-option v-for="(dict, index) in replace" :key="index"
                :label="`${dict.materialName}(${dict.materialNum})`" :value="dict.ecrRecordId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <div align="center" slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm()">确 定</el-button>
        <el-button @click="() => $parent.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getSelectChildrenupkeep, getSelectReplaceRecordList, putOriginCompleted } from "@/api/maintenance/insurance/insurance.js";
// import Upload from '@/components/upload/index.vue'
export default {
  props: ['rowObj'],
  components: {
    // Upload
  },
  data () {
    return {
      form: {

      },
      formData: {
        ecrRemoveRecords: []
      },
      replace: [],
      rules: {
        ecrRemoveRecords: { required: false, message: '必填', trigger: 'blur' },
      }
    }
  },
  created () {
    this.getlist()
  },
  methods: {
    handleChange (index, key, value) {
      if (value === '0') {
        this.form.upkeepTaskStatuses[index].upkeepTaskGenerProVos[key].explanation = ''
      }
    },
    async getlist () {
      let res = await getSelectChildrenupkeep(this.rowObj.upkeepTaskId, this.rowObj.equipmentAccountId);//保养任务ID/设备台账ID
      let promise = {
        equipmentAccountId: this.rowObj.equipmentAccountId,
        status: '0',
        useNum: this.rowObj.upkeepTaskNum
      }
      let param = await getSelectReplaceRecordList(promise);
      this.replace = param.data
      this.form = res.data
    },
    //提交
    submitForm () {

      this.$refs['formData'].validate((valid) => {
        if (!valid) return false
        let ArrPros = []
        for (let index = 0; index < this.form.upkeepTaskStatuses.length; index++) {
          const element = this.form.upkeepTaskStatuses[index];
          for (let j = 0; j < element.upkeepTaskGenerProVos.length; j++) {
            const item = element.upkeepTaskGenerProVos[j];
            if (item.status !== '1') {
              return this.$message.error('所有保养状态必须为确认状态')
              // throw new Error('所有保养状态必须为确认状态')
            }
            ArrPros.push({
              utgpId: item.utgpId,
              status: item.status,
              explanation: item.explanation
            })
          }
        }
        let params = {
          upkeepTaskId: this.rowObj.upkeepTaskId,
          ecrRemoveRecords: this.formData.ecrRemoveRecords.map(item => { return { ecrRecordId: item } }),
          upkeepTaskGenerProVos: ArrPros
        }
        putOriginCompleted(params).then(res => {
          if (res.code === 200) {
            this.$message.success(res.msg)
            this.$parent.open = false
            this.$parent.getList()//重新查询列表
          }
        })

      });
    },
  }
}
</script>

<style lang="scss" scoped>
.center_dialog {
  // text-align: center;
  width: 90%;
  margin: 0px auto;
}
</style>